var logger = require('pomelo-logger').getLogger('crash-log');
module.exports = function(app) {
  return new Handler(app);
};

var Handler = function(app) {
  this.app = app;
};
Handler.prototype.test = function(msg, session, next) {
	next(null, {
		code: 200,
		txt:"hello, wakakaka" 
	});
}

/* 第一次登陆, 就给他发一个connectorid 然后完事, 让他自己去连接connector */
Handler.prototype.queryEntry = function(msg, session, next) {
	var uid = msg.rid;
	if(!uid) {
		next(null, {
			code: 501
		});
		return;
	}
	// get all connectors
	var connectors = this.app.getServersByType('connector');
	
	//logger.info("[" + _tag + ":" + _level + "]" + connectors.length);

	if(!connectors || connectors.length === 0) {
		next(null, {
			code: 500, 
		});
		return;
	}
	// here we just start `ONE` connector server, so we return the connectors[0] 
	var res = connectors[0];
	next(null, {
		code: 200,
		host: "40.81.29.216", //res.host,
		port: res.clientPort
	});
};
